package com.ccjq.shosestore.member.dao.impl;

import com.ccjq.shosestore.core.dbmanager.DbConnection;
import com.ccjq.shosestore.member.dao.UserDao;
import com.ccjq.shosestore.member.po.UserModel;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    Connection conn=null;
    public int save(UserModel model) {
        int result =0;
        String sql = "insert into t_user values(?,?,?,?)";
        conn = DbConnection.getConnection();
        PreparedStatement ps =null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,model.getUserId());
            ps.setString(2,model.getUsername());
            ps.setString(3,model.getPassword());
            ps.setString(4,model.getMobilePhone());
            result =ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DbConnection.close(null,ps,conn);
        }
        return result;
    }

    public int update(UserModel model) {
        int result =0;
        String sql = "update t_user set username =?,password =?,mobile_phone =? where user_id =?";
        conn = DbConnection.getConnection();
        PreparedStatement ps =null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,model.getUsername());
            ps.setString(2,model.getPassword());
            ps.setString(3,model.getMobilePhone());
            ps.setString(4,model.getUserId());
            result =ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DbConnection.close(null,ps,conn);
        }
        return result;
    }

    public int delete(String userId) {
        int result =0;
        String sql = "delete from t_user where user_id ='"+userId+"'";
        conn = DbConnection.getConnection();
        PreparedStatement ps =null;
        try {
            ps = conn.prepareStatement(sql);
            result =ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DbConnection.close(null,ps,conn);
        }
        return result;
    }

    public UserModel findUserById(String userId) {
        UserModel model =null;
        String sql = "select user_id,username,password,mobile_phone from t_user where user_id='"+userId+"'";
        conn = DbConnection.getConnection();
        PreparedStatement ps =null;
        ResultSet rs =null;
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            if(rs.next())
            {
                model  = new UserModel();
                model.setUserId(rs.getString(1));
                model.setUsername(rs.getString(2));
                model.setPassword(rs.getString(3));
                model.setMobilePhone(rs.getString(4));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DbConnection.close(rs,ps,conn);
        }

        return model;
    }

    public UserModel findUserByNamePw(String username, String password) {
        UserModel model =null;
        String sql = "select user_id,username,password,mobile_phone from t_user where username='"+username+"' and password='"+password+"'";
        conn = DbConnection.getConnection();
        PreparedStatement ps =null;
        ResultSet rs =null;
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            if(rs.next())
            {
                model  = new UserModel();
                model.setUserId(rs.getString(1));
                model.setUsername(rs.getString(2));
                model.setPassword(rs.getString("password"));
                model.setMobilePhone(rs.getString(4));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DbConnection.close(rs,ps,conn);
        }

        return model;
    }

    public UserModel findUserByName(String username) {
        UserModel model =null;
        String sql = "select user_id,username,password,mobile_phone from t_user where username='"+username+"'";
        conn = DbConnection.getConnection();
        PreparedStatement ps =null;
        ResultSet rs =null;
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            if(rs.next())
            {
                model  = new UserModel();
                model.setUserId(rs.getString(1));
                model.setUsername(rs.getString("username"));
                model.setPassword(rs.getString(3));
                model.setMobilePhone(rs.getString(4));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DbConnection.close(rs,ps,conn);
        }

        return model;
    }
}
